package com.xm.clj.underserver.controller;



import com.alibaba.fastjson.JSON;
import com.xm.clj.underserver.bean.GetMessage;
import com.xm.clj.underserver.bean.LogUser;
import com.xm.clj.underserver.bean.RegUser;
import com.xm.clj.underserver.utils.SendMessage;
import com.xm.clj.underserver.utils.UserInfo;
import net.sf.json.JSONObject;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

@WebServlet(name = "user",urlPatterns = {"/reg","/log"})
public class Register extends HttpServlet {

    @Autowired
    JdbcTemplate j;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        System.out.println("getting");
//        //用户登录
//        InputStream is = req.getInputStream();
//        String str = IOUtils.toString(is,"UTF-8");
//        JSONObject jsonObject = JSONObject.fromObject(str);
//        User user = (User) JSONObject.toBean(jsonObject,User.class);
//        GetMessage getMessage = null;
//        //查找数据库验证用户信息
//        int res = UserInfo.doLogin(user,j);
//        switch (res){
//            case 0 : getMessage = SendMessage.SUCCESS;break;
//            case 1 : getMessage = SendMessage.LogError1;break;
//            case 2 : getMessage = SendMessage.LogError2;break;
//            case 3 : getMessage = SendMessage.LogError3;break;
//        }
//        resp.getWriter().write(JSON.toJSONString(getMessage));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("start reg !!!!!!!!!");
        resp.setCharacterEncoding("UTF-8");
        resp.setHeader("Content-type", "text/html;charset=UTF-8");
        InputStream is = req.getInputStream();
        String str = IOUtils.toString(is,"UTF-8");
        String path = req.getServletPath();
        System.out.println(path);
        GetMessage getMessage = null;
        if (path.equals("/reg")) {
            JSONObject jsonObject = JSONObject.fromObject(str);
            RegUser regUser = (RegUser) JSONObject.toBean(jsonObject, RegUser.class);
            // 注册用户
            // 信息检查
            int res = UserInfo.doRegUser(regUser, j);
            System.out.println("res: " + res);
            switch (res) {
                case 0:
                    getMessage = SendMessage.SUCCESS;
                    break;
                case 1:
                    getMessage = SendMessage.RegError1;
                    break;
                case 2:
                    getMessage = SendMessage.RegError2;
                    break;
                case 3:
                    getMessage = SendMessage.RegError3;
                    break;
                case 4:
                    getMessage = SendMessage.RegError4;
                    break;
            }
        }
        if (path.equals("/log")) {
            JSONObject jsonObject = JSONObject.fromObject(str);
            System.out.println(str);
            LogUser logUser = (LogUser) JSONObject.toBean(jsonObject, LogUser.class);
            //查找数据库验证用户信息
            int res = UserInfo.doLogin(logUser, j);
            switch (res) {
                case 0:
                    getMessage = SendMessage.SUCCESS;
                    break;
                case 1:
                    getMessage = SendMessage.LogError1;
                    break;
                case 2:
                    getMessage = SendMessage.LogError2;
                    break;
                case 3:
                    getMessage = SendMessage.LogError3;
                    break;
            }
            System.out.println(res + "      "+getMessage.getMes());
        }
        resp.getWriter().write(JSON.toJSONString(getMessage));
    }

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPut(req, resp);
    }

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doDelete(req, resp);
    }
}
